/**
 * aop工厂
 *
 * 处理类一个
 * 实体类一个
 *
 * 返回被aop过的处理过的类
 */
export default class AopFactory{
    constructor(){
        //es5转 es6的选项  import 加 注解 @ decorator
        const inBrowser = typeof window !== 'undefined'
        this.inBrowser=inBrowser;
        if( inBrowser){
            this. options= {
                "plugins": [
                    ["proposal-decorators" , { "legacy": true }],
                    ["syntax-dynamic-import"]

                ],
                presets: ['react']
            }

            this.babel=Babel;
            this.load=this.loadJs;
        }else {
            this. options= { presets: [ '@babel/env' ],
                "plugins": [
                    ["@babel/plugin-proposal-decorators", { "legacy": true }],
                    ["@babel/plugin-syntax-dynamic-import"]

                ]
            }
            this.babel=require("@babel/core");
            this.load=this.requireFromString;
        }



    }
    //nodejs 字符串动态加载
    requireFromString(jarName,name,code) {
        var Module = module.constructor;
        var m = new Module();
        let funcName="_"+jarName+"_"+name
        m._compile(funcName, code);
        return m.exports;
    }
    //web 加载
      loadJs(jarName,name,code){
          let rname=name .replace(".", '_')
        let root=qguan('root');
        let funcName="_"+jarName+"_"+rname
       code=   `(function`+funcName+`(module, exports, __webpack_require__) { 
`  + code+  `})`
          var modules=[];
          modules[1]=  eval(code);
          var installedModules = {};
          // The require function
          /******/ 	function __webpack_require__(moduleId) {
              /******/
              /******/ 		// Check if module is in cache
              /******/ 		if(installedModules[moduleId]) {
                  /******/ 			return installedModules[moduleId].exports;
                  /******/ 		}
              /******/ 		// Create a new module (and put it into the cache)
              /******/ 		var module = installedModules[moduleId] = {
                  /******/ 			i: moduleId,
                  /******/ 			l: false,
                  /******/ 			exports: {}
                  /******/ 		};
              /******/
              /******/ 		// Execute the module function
              /******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
              /******/
              /******/ 		// Flag the module as loaded
              /******/ 		module.l = true;
              /******/
              /******/ 		// Return the exports of the module
              /******/ 		return module.exports;
              /******/ 	}

          let obj =__webpack_require__(1) ;
        return obj;
    }


    /**
     * 通过code直接创建
     */
    createCodeObj(jarName,name,code){
        if(this.inBrowser){
            let result = this.babel.transform(
                code,this.options
            );
            let codeobj=  this.loadJs(jarName,name,result.code);
            return codeobj;
        }else {
            this.babel.transformAsync(code, this.options).then(result => {
                let codeobj=  this.loadJs(jarName,name,result.code);
                return codeobj;
            });
        }

    }
    //生成普通实体类
    createObj(name,target) {
        //是否AOP
        if (qguan.aop) {
            let obj=  new target();
            return    this.createAopObj(name,obj,{});
        }else {
            return new target();
        }
    }


    //生成 aop 的实体类
    createAopObj(name,obj,reg){
        let AopProxy=qguan("aopproxy");
        let  regf = qguan(name,obj,reg,'aopContext')
        let  aopProxy= new  AopProxy();
        aopProxy.aopFuns=regf
        return  new Proxy(obj, aopProxy)

    }
}

